package com.sgb.kjwl.viewmodel.self;

import androidx.databinding.ObservableField;
import android.widget.Toast;

import com.sgb.kjwl.AppConstant;
import com.sgb.kjwl.model.entity.user.SupplierAuthEntity;
import com.sgb.kjwl.repository.SelfRepertory;
import com.sgb.kjwl.view.ui.activity.self.authentication.CertificationActivity;
import com.sgb.kjwl.view.ui.activity.self.authentication.OpenTicksInfoActivity;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MLog;

import io.reactivex.functions.Consumer;

public class OpenTicksModel extends BaseViewModel {
    public ObservableField<SupplierAuthEntity> supplierAuthEntity = new ObservableField<>();
    private OpenTicksInfoActivity activity;
    private SelfRepertory repertory;

    public OpenTicksModel(OpenTicksInfoActivity activity, SelfRepertory repertory) {
        this.activity = activity;
        this.repertory = repertory;
    }

    //提交认证信息
    public void submitAuthInfoModel(SupplierAuthEntity supplierAuthEntity) {
        DialogHelper.showProgressDialog(activity, null, "提交中...", 0, false, null).setCanceledOnTouchOutside(false);
        repertory.addSupplierAuth(supplierAuthEntity)
                .subscribe(new Consumer<BaseEntity>() {
                    @Override
                    public void accept(BaseEntity baseEntity) throws Exception {
                        DialogHelper.dismissProgressDialog();
                        MLog.d(AppConstant.API_LOG, "auth " + baseEntity.getCode() + " " + baseEntity.getData() + "  " + baseEntity.getMsg());
                        if (baseEntity.getCode().equals("200")) {
                            Toast.makeText(activity, "提交认证成功！", Toast.LENGTH_SHORT).show();
                            CertificationActivity.start(activity);
                        } else {
                            Toast.makeText(activity, baseEntity.getMsg(), Toast.LENGTH_SHORT).show();
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        MLog.d(AppConstant.API_LOG, "error " + throwable.getMessage());
                    }
                });
    }
}
